import { NodeSSH } from "node-ssh";
import { Config } from "../../types/config";
import { Once } from "../../utils/functional";
import { withDefaults } from "../../utils/withDefaults";

function resolveSSH() {
    const ssh = new NodeSSH();

    // 持久连接
    const connect = ssh.connect;
    ssh.connect = Once((config: Parameters<typeof connect>[0]) => {
        return connect.call(ssh, config);
    });

    return ssh;
}

export function resolveOptionSshList(config: Pick<Config, "sshList">) {
    const { sshList } = config;
    return sshList.map((ssh) => {
        return withDefaults(ssh, {
            username: "root",
            ssh: resolveSSH(),
        });
    });
}
